﻿<%@ page contentType="text/html; charset=utf-8" %>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%@page import="java.io.*"%>
<%

System.out.println("yes");
String userName="root";
String userPasswd="";
String dbName="a0713225307";

request.setCharacterEncoding("UTF-8");

Object uname=session.getAttribute("uname");

String txt=request.getParameter("message");

java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

java.util.Date currentTime = new java.util.Date();//得到当前系统时间

String str_date = formatter.format(currentTime); //将日期时间格局化

try{
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	String u="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd+"&useUnicode=true&characterEncoding=UTF-8";
	java.sql.Connection con=java.sql.DriverManager.getConnection(u,"root","");
	String sql="insert into message_db(time,uNam,txt)values('"+str_date+"','"+uname+"','"+txt+"')";
	
	Statement statement=con.createStatement();
	
	if(statement.executeUpdate(sql)==1){
		
		System.out.println("success");
		response.sendRedirect("user.jsp");
	
	}else{
		System.out.println("fail");

	}
	
     if(statement!=null){
  statement.close();
  }
  if(con!=null){
  con.close();
  }
  }catch(Exception e){
   e.printStackTrace();
   System.out.println(e);
  } 

%>